
import requests

ip_url1 ='http://httpbin.org/ip'
ip_url2 ='http://ipinfo.io/ip'
ip_url3 ='http://ipecho.net/plain'
ip_url4 ='https://www.ip.cn/api/index?ip=&type=0'


header = {
    "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36"
}

def from_httpbin_get():
    req = requests.get(ip_url1,headers=header)
    res = req.json()['origin']
    print(res)
    return res

def from_ipinfo_get():
    req = requests.get(ip_url2,headers=header)
    res = req.text
    print(res)
    return res

def from_ipecho_get():
    req = requests.get(ip_url3,headers=header)
    res = req.text
    print(res)
    return res

def from_ip_get():
    req = requests.get(ip_url4,headers=header)
    res = req.json()['ip']
    print(res)
    return res

# from_httpbin_get()
# from_ipinfo_get()
# from_ipecho_get()
# from_ip_get()

def isConnect():
    try:
        html=requests.get('http://www.baidu.com',timeout=2)
    except:
        return False
    return True

def get_server_iplist():
    try:
        req = requests.get('https://gitee.com/ewaimp/ip/raw/master/home.json')
        if req.status_code==200:
            return {
                'code':req.status_code,
                'result':req.text
            }
        else:
            return {
                'code':req.status_code,
                'result':'网络路径或者配置文件错误'
            }
    except :
        return {
            'code': 400,
            'result': '请求url不通'
        }

def parse_Iplist():
    r = get_server_iplist()
    if r['code'] == 200:
        return eval(r['result'])['ip']


def send_code( _addr,_code):
    req = requests.get(_addr+'/'+_code,headers=header,timeout=3)
    print('指令返回结果:')
    print(req.text)

if __name__ == '__main__':
    iplist=[]
    ip='192.168.0.145'
    port='8006'
    addr = 'http://'+ ip + ':' + port
    if isConnect():
        iplist = parse_Iplist()
        print(iplist)

        send_code(addr,'aaa')



